package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.bean.HomeBannerBean;
import com.mwx.baselib.bean.PlayListBean;
import com.mwx.baselib.bean.PlayListItemBean;
import com.mwx.baselib.bean.SceneListBean;
import com.mwx.baselib.bean.SceneSongBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.bean.TagListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.utils.MLog;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentFeaturedListBinding;
import com.mwx.baselib.base.BaseFragment;
import com.zhanbo.zhanbowymusic.dialog.ScenePop;
import com.zhanbo.zhanbowymusic.main.bean.FeaturedPlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.PCZoneListEBBEan;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedRecommendedPlayListAdapter;
import com.zhanbo.zhanbowymusic.main.featured.adapter.HomeFeaturedBannerAdapter;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedHomeHotPodcastListAdapter;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedHomeNewMusicListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 精选列表
 */
public class FeaturedListFragment extends BaseFragment<FragmentFeaturedListBinding> implements View.OnClickListener {


    private List<Integer> newMusicdata;
    private List<SceneListBean.ItemsDTO> sceneList;
    private FeaturedRecommendedPlayListAdapter featuredRecommendedPlayListAdapter;
    private FeaturedHomeNewMusicListAdapter featuredHomeNewMusicListAdapter;
    private ScenePop scenePop;

    @Override
    protected Object getLayout() {
        MLog.e("R.layout.fragment_featured_list");
        return R.layout.fragment_featured_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        initBanner(); //banner
        initRecommendedplaylists(); //推荐歌单
        initNewMusic(); //最新音乐
        initSceneList(); //场景列表
        tagList();
    }

    /**
     * 场景列表
     */
    private void initSceneList() {
        Map<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("size", "7");
        NetworkManager.getInstance().getApiService().sceneList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<SceneListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<SceneListBean> listBaseResponses) {
                        if (null != listBaseResponses.getData().getItems() && listBaseResponses.getData().getItems().size() > 0) {
                            sceneList = listBaseResponses.getData().getItems();
                            scenePop.setSceneList(sceneList);
                            GlideUtils.loadImage(_mActivity, sceneList.get(0).getImage(), binding.ivScene);
                        }
                    }
                });
    }

    /**
     * 标签 亲子专区
     */
    private void tagList() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        NetworkManager.getInstance().getApiService().tagList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<TagListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<TagListBean> listBaseResponses) {
                        if (null != listBaseResponses.getData().getItems() && listBaseResponses.getData().getItems().size() > 0) {
                            GlideUtils.loadImageRounded10(_mActivity, listBaseResponses.getData().getItems().get(0).getImage(), binding.ivTag);
                        }
                    }
                });
    }

    /**
     * 最新音乐
     */
    private void initNewMusic() {
        Map<String, String> map = new HashMap<>();
        NetworkManager.getInstance().getApiService().recommend(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<SongBean>>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<List<SongBean>> listBaseResponses) {

                        featuredHomeNewMusicListAdapter.getItems().addAll(listBaseResponses.getData());
                        binding.rvNewMusic.setAdapter(featuredHomeNewMusicListAdapter);
                    }
                });
    }

    //推荐歌单
    private void initRecommendedplaylists() {
        Map<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("size", "6");
        map.put("isRecommend", "1");

        NetworkManager.getInstance().getApiService().playlist(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<PlayListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<PlayListBean> listBaseResponses) {

                        featuredRecommendedPlayListAdapter.getItems().addAll(listBaseResponses.getData().getItems());
                        binding.rvRecommended.setAdapter(featuredRecommendedPlayListAdapter);
                    }
                });
    }

    @Override
    protected void initView() {
        scenePop = new ScenePop(_mActivity);
        //热门播客
        newMusicdata = new ArrayList<>();
        binding.rvHotPodcast.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        FeaturedHomeHotPodcastListAdapter featuredHomeHotPodcastListAdapter = new FeaturedHomeHotPodcastListAdapter(_mActivity);
        featuredHomeHotPodcastListAdapter.getItems().addAll(newMusicdata);
        binding.rvHotPodcast.setAdapter(featuredHomeHotPodcastListAdapter);
        //推荐歌单
        binding.rvRecommended.setLayoutManager(new LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false));
        featuredRecommendedPlayListAdapter = new FeaturedRecommendedPlayListAdapter(_mActivity);
        //最新音乐
        binding.rvNewMusic.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        featuredHomeNewMusicListAdapter = new FeaturedHomeNewMusicListAdapter(_mActivity);
    }

    //初始化banner
    private void initBanner() {
        Map<String, String> map = new HashMap<>();
        NetworkManager.getInstance().getApiService().getHomeBanner(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<HomeBannerBean>>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<List<HomeBannerBean>> listBaseResponses) {

                        HomeFeaturedBannerAdapter bannerAdapter = new HomeFeaturedBannerAdapter(_mActivity, listBaseResponses.getData());
                        binding.banner.addBannerLifecycleObserver(_mActivity)//添加生命周期观察者
                                .setAdapter(bannerAdapter)
                                .setIndicator(new CircleIndicator(_mActivity));
                        binding.banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                EventBus.getDefault().post(new FeaturedPlaylistDetailsEBBean(featuredRecommendedPlayListAdapter.getItems().get(0)));
                            }
                        });
                    }
                });
    }

    @Override
    protected void initListener() {
        binding.ivSelectScene.setOnClickListener(this);
        binding.pCZone.setOnClickListener(this);
        featuredRecommendedPlayListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<PlayListItemBean>() {
            @Override
            public void onItemClick(int position) {
                EventBus.getDefault().post(new FeaturedPlaylistDetailsEBBean(featuredRecommendedPlayListAdapter.getItems().get(position)));
            }
        });
        featuredHomeNewMusicListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
            @Override
            public void onItemClick(int position) {
                MusicController.getInstance().updateItemDate(featuredHomeNewMusicListAdapter.getItems().get(position));
            }
        });
        //场景点击事件
        scenePop.setOnItemClickListener(new ScenePop.ScenePopOnItemClickListener() {
            @Override
            public void onItemClick(SceneListBean.ItemsDTO itemsDTO) {
                getSceneSong(itemsDTO);
                scenePop.dismiss();
            }
        });


    }

    /**
     * 获取场景歌单
     */
    private void getSceneSong(SceneListBean.ItemsDTO itemsDTO) {
        GlideUtils.loadImageRounded10(_mActivity, itemsDTO.getImage(), binding.ivScene);
        NetworkManager.getInstance().getApiService().sceneSong(itemsDTO.getId())
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<SceneSongBean>>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<List<SceneSongBean>> listBaseResponses) {
                        List<SongBean> songBeans = new ArrayList<>();
                        for (SceneSongBean datum : listBaseResponses.getData()) {
                            songBeans.add(datum.getSong());
                        }
                        MusicController.getInstance().updateAndPlaySheetData(songBeans);
                    }
                });

    }


    public static FeaturedListFragment newInstance() {

        Bundle args = new Bundle();

        FeaturedListFragment fragment = new FeaturedListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_select_scene:
                scenePop.showAsDropDown(binding.ivSelectScene, -500, 0);
                break;
            case R.id.p_c_zone:
                //亲子专区
                EventBus.getDefault().post(new PCZoneListEBBEan());
                break;
            default:
                EventBus.getDefault().post(new FeaturedPlaylistDetailsEBBean());
                break;
        }

    }
}